feat(docker): optimize concurrency performance and memory management#1689
feat(docker): optimize concurrency performance and memory management#1689mzyfree wants to merge 4 commits intounclecode:developfrom
Conversation
This commit consolidates several optimizations for crawl4ai in high-concurrency environments: 1. Browser Pool Optimization: - Implemented a tiered browser pool (Hot, Cold, Retired). - Added a browser retirement mechanism based on usage count (MAX_USAGE_COUNT) and memory pressure (MEMORY_RETIRE_THRESHOLD). - Added reference counting (active_requests) to ensure browser instances are not closed while in use. - Enhanced the pool janitor with adaptive cleanup intervals based on system memory. 2. Resource Loading Optimization: - Integrated optional CSS and Ad blocking to reduce memory footprint and improve QPS. - Decoupled resource filtering from text_mode to allow granular control. 3. Stability and Scalability: - Added mandatory release_crawler calls in API/Server handlers to prevent resource leaks. - Introduced environment variables to toggle these new features (defaulting to False for safe community adoption). - Added optional 5-minute pool audit logs for better observability. Co-authored-by: dylan.min <dylan.min@example.com>
…eanup docs - Refactor BrowserManager to dynamically block resources based on avoid_css and text_mode - Align text_mode behavior with community standards (no forced CSS blocking) - Add Top 20 curated ad and tracker patterns for performance - Restore and translate permanent browser logs in crawler_pool.py - Clean up models.py schema annotations and server.py docstrings - Add unit and functional tests for filtering flags
01fb7ee to
47bc688
Compare
|
@unclecode @ntohidi please review this MR |
|
@mzyfree +1, better supported in high concurrence envs is needed |
|
Look forward to it as well! The current performance is very POOR. The QPS is <1 for 2 CPU + 4GB RAM, tried to fetch 3 URLs in one request |
|
@ntohidi @aravindkarnam Pls help... |
|
Any update pls? |
|
Anyone is looking at this issue pls? |
|
@unclecode @ntohidi @ara |
|
@mzyfree Thx for this PR, u have done a good job here, I will review this soon. Sorry for later reply, been very busy preparing our hosted platform for Crawl4ai. @AlbertInRC thx mentioning me on this. |
Resolve conflicts in async_configs and docker server while keeping avoid_ads/avoid_css and upstream init_scripts, and retaining upstream URL scheme validation.
|
|
@mzyfree Thanks for this excellent PR - the analysis of pool-level resource leaks and the We've been doing a lot of internal refactoring on the browser manager and pool layers recently, so rather than merging this directly (it would need significant rebasing), we've implemented the core ideas from your PR ourselves, adapted to the current codebase:
We intentionally left out the browser retirement mechanism for now since it overlaps with our existing These changes are already pushed to the |
…ecycle Add opt-in BrowserConfig flags (avoid_ads, avoid_css) for blocking ad/tracker domains and CSS resources at the browser context level. Refactor crawler pool with release_crawler() and active_requests tracking to prevent janitor from closing browsers with in-flight requests. Add proper finally blocks to all Docker API/server handlers. Update docs for new config options. Inspired by #1689.
|
Thanks for the contribution! This fix has already been implemented on the develop branch via a different approach and will be in the next release. Closing as superseded - appreciate your effort! |
Summary
This PR introduces a comprehensive optimization suite for
crawl4aiin high-concurrency Docker environments. It focuses on improving QPS (Queries Per Second) and ensuring long-term memory stability by re-engineering the browser pooling mechanism and introducing optional resource filtering.Key Design Principle: All new features are opt-in. By default, the system behaves exactly as before, ensuring zero impact on existing community users.
Core Enhancements:
active_requeststracking to prevent browsers from being closed while still processing requests, fixing common "Target closed" errors under load.New Configuration Options
These new features can be enabled via
BrowserConfigor Environment Variables:Engine Layer (
BrowserConfig)avoid_ads(bool, default:False): Enable intercepting and blocking ad/tracker network requests.avoid_css(bool, default:False): Enable blocking CSS resource loading to save CPU/Memory.Docker Layer (Environment Variables)
CRAWL4AI_BROWSER_RETIREMENT_ENABLED(default:false): Enable the usage/memory-based retirement mechanism.CRAWL4AI_PERMANENT_BROWSER_DISABLED(default:false): If true, disables the always-on permanent browser instance.CRAWL4AI_POOL_AUDIT_ENABLED(default:false): Enable detailed pool status logging every 5 minutes.CRAWL4AI_BROWSER_MAX_USAGE(default:100): Max requests per instance before retirement.CRAWL4AI_MEMORY_RETIRE_THRESHOLD(default:75): System memory % to trigger aggressive retirement.List of files changed and why
crawl4ai/async_configs.py: Added new parameters toBrowserConfig.crawl4ai/browser_manager.py: Implemented the network interception logic for resource filtering.deploy/docker/crawler_pool.py: Implemented the tiered pool, retirement, and audit logic.deploy/docker/api.py&deploy/docker/server.py: Updated withtry...finallyfor accurate reference counting.How Has This Been Tested?
False) values.Checklist:
Stress test performance
QPS increased by 40%
Resource with no OOM